local minxin = fk.CreateSkill{
  name = "hanqing__minxin",
}

Fk:loadTranslationTable{
  ["hanqing__minxin"] = "民信",
  [":hanqing__minxin"] = "每轮限一次，一名角色进行“<a href='zhengsu_desc'>整肃</a>”时，你可以为其选择“擂进”，"..
  "且翻倍其此次“<a href='zhengsu_desc'>整肃</a>”奖励；"..
  "若场上一半以上的角色获得过“<a href='zhengsu_desc'>整肃</a>”奖励，本局游戏内你获得“<a href='zhengsu_desc'>整肃</a>”奖励后，"..
  "可以视为使用一张【杀】。",

  ["#hanqing__minxin-invoke"] = "民信：是否令 %dest 本次“%arg”整肃改为“擂进”并翻倍奖励？",
  ["#hanqing__minxin-slash"] = "民信：你可以视为使用【杀】",
}

local U = require "packages/utility/utility"

minxin:addEffect(U.ZhengsuRewarded, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(minxin.name) and
      #table.filter(player:getTableMark(minxin.name), function (id)
        return not player.room:getPlayerById(id).dead
      end) * 2 > #player.room.alive_players and
      player:canUse(Fk:cloneCard("slash"), { bypass_times = true })
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = "slash",
      skill_name = minxin.name,
      prompt = "#hanqing__minxin-slash",
      cancelable = true,
      extra_data = {
        bypass_times = true,
        extraUse = true,
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).extra_data)
  end,

  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(minxin.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addTableMarkIfNeed(player, minxin.name, target.id)
  end,
})

minxin:addEffect(U.StartZhengsu, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(minxin.name) and
      data.choice ~= "zhengsu_leijin" and
      player:usedEffectTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = minxin.name,
      prompt = "#hanqing__minxin-invoke::"..target.id..":"..data.skill_name,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    data.choice = "zhengsu_leijin"
    player.room:setPlayerMark(target, "hanqing__minxin-turn", data.skill_name)
  end,
})

minxin:addEffect(U.ZhengsuRewarding, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("hanqing__minxin-turn") == data.skill_name
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    data.extra_data = data.extra_data or {}
    data.extra_data.hanqing__minxin = data.reward
    data.reward = function ()
      data.extra_data = data.extra_data or {}
      if data.extra_data.hanqing__minxin == "draw2" then
        data.target:drawCards(4, data.skill_name)
      elseif data.extra_data.hanqing__minxin == "recover" then
        room:recover{
          who = data.target,
          num = 2,
          recoverBy = data.target,
          skillName = data.skill_name,
        }
      end
    end
  end,
})

return minxin
